﻿using System;
using System.IO.IsolatedStorage;
using System.Windows;
using PhoneAppRssReader.Entity;
using PhoneAppRssReader.Utls;

namespace PhoneAppRssReader
{
    public partial class AddRssPage
    {
        public AddRssPage()
        {
            InitializeComponent();
            TitleTextBox.Focus();
        }


        private void SaveRss(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            var newFeed = new Feed { Id = XmlAction.NextId(), Title = TitleTextBox.Text, Url = LinkTextBox.Text };
            var success = XmlAction.SaveFeed(newFeed);
            if (success)
            {
                MessageBox.Show("Write success");
                App.Source.Items.Add(newFeed);
                Reset(sender, e);
            }
            else
            {
                MessageBox.Show("Failure");
            }

        }

        private void Reset(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            TitleTextBox.Text = string.Empty;
            LinkTextBox.Text = "http://";
            TitleTextBox.Focus();
        }

        private void Home(object sender, EventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ManagerRssFeed(object sender, System.EventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.Navigate(new Uri("/ManagerRssFeedPage.xaml", UriKind.Relative));
        }

        private void AddSampleData(object sender, System.EventArgs e)
        {
            // TODO: Add event handler implementation here.
            var success = XmlAction.SaveFeed(FeedSource.GetSampleDataRss());
            if (!success) return;
            //Reload LoadData in App._source
            App.Source.IsDataLoad = false;

            MessageBox.Show("Load Sample data success");
            //Reload items rss feeds
            App.Source.Items.Clear();
            App.Source.LoadData();
        }
    }
}
